﻿using Fidely.Framework;
using Sasa.QualityTools.CodeAnalysis.Core.Models;
using System;
using System.ComponentModel;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.ViewModels
{
    public class ProblemViewModel : ViewModelBase, ICodeAnalysisResultRowViewModel
    {
        private Problem problem;

        private string projectName;

        private string fileName;


        [NotEvaluate]
        public string Id
        {
            get { return problem.Id; }
            set { problem.Id = value; OnPropertyChanged("Id"); }
        }

        [Rename("id")]
        [Description("CheckId of FxCop rule")]
        public string CheckId
        {
            get { return problem.CheckId; }
            set { problem.CheckId = value; OnPropertyChanged("CheckId"); }
        }

        [Rename("category")]
        [Description("Category of FxCop rule")]
        [Alias("c", Description = "Alias name of 'category'")]
        public string Category
        {
            get { return problem.Category; }
            set { problem.Category = value; OnPropertyChanged("Category"); }
        }

        [Rename("project")]
        [Description("Project name")]
        [Alias("p", Description = "Alias name of 'project'")]
        public string ProjectName
        {
            get { return projectName; }
            set { projectName = value; OnPropertyChanged("ProjectName"); }
        }

        [NotEvaluate]
        public string Path
        {
            get { return problem.Path; }
            set
            {
                problem.Path = value;
                FileName = (value != null) ? System.IO.Path.GetFileName(value) : null;
                OnPropertyChanged("Path");
            }
        }

        [Rename("file")]
        [Description("File Name")]
        [Alias("f", Description = "Alias name of 'file'")]
        public string FileName
        {
            get { return fileName; }
            private set { fileName = value; OnPropertyChanged("FileName"); }
        }

        [NotEvaluate]
        public int? Line
        {
            get { return problem.Line; }
            set { problem.Line = value; OnPropertyChanged("Line"); }
        }

        [Rename("message")]
        [Description("Vliolation message.")]
        [Alias("m", Description="Alias name of 'message'")]
        public string Message
        {
            get { return problem.Message; }
            set { problem.Message = value; OnPropertyChanged("Message"); }
        }

        [NotEvaluate]
        public string AssemblyPath
        {
            get { return problem.AssemblyPath; }
            set { problem.AssemblyPath = value; OnPropertyChanged("AssemblyPath"); }
        }

        [NotEvaluate]
        public string TypeName
        {
            get { return problem.TypeName; }
            set { problem.TypeName = value; OnPropertyChanged("TypeName"); }
        }

        [Rename("kind")]
        [Description("Kind of target.")]
        [Alias("k", Description = "Alias name of 'kind'")]
        public string Kind
        {
            get { return Enum.GetName(typeof(CodeKind), problem.Kind); }
            set { problem.Kind = (CodeKind)Enum.Parse(typeof(CodeKind), value); OnPropertyChanged("Kind"); }
        }


        public ProblemViewModel()
            : this(new Problem())
        {
        }

        public ProblemViewModel(Problem problem)
        {
            this.problem = problem;
            FileName = (problem.Path != null) ? System.IO.Path.GetFileName(problem.Path) : null;
        }


        public override string ToString()
        {
            var values = new string[] { Kind.ToString(), CheckId, Category, ProjectName, FileName, Message };
            return String.Join(";", values);
        }
    }
}
